/*
 * @Author: your name
 * @Date: 2022-03-10 21:51:27
 * @LastEditTime: 2022-05-02 10:06:59
 * @LastEditors: Please set LastEditors
 * @Description: 登录注册接口
 * @FilePath: \blog-system-serve\Routes\user.js
 */
const express = require("express");
const path = require("path");
const model = require(path.join(__dirname, "../Model/model"));
const router = express.Router();
const bcryptjs = require("bcryptjs");
const db = require("../Model/model");
const jsonToken = require("jsonwebtoken");
/**
 * @description: 登录接口
 * @param {*} 用户名，密码`
 * @return {*}
 */
router.get("/login", (req, res, err) => {
  const { username, password } = req.query;
  const querySql = `SELECT * from user_info where username ="${username}"`;
  db(querySql, (err, result) => {
    if (err) return err;
    if (result.length === 0) {
      res.send({
        message: "用户不存在",
        failed: true,
      });
    } else {
      const sqlPsd = result[0].password;
      const psd = bcryptjs.compareSync(password, sqlPsd);   
      if (psd) {
        const secret = "hand";
        // 3.生成token字符串
        const token = jsonToken.sign({ username: username }, secret, {
          expiresIn: "10h",
        });
        res.send({
          message: "登录成功",
          username,
          token,
          failed: false,
        });
      } else {
        res.send({
          message: "用户名密码不一致",
          failed: true,
        });
      }
    }
  });
});
/**
 * @description: 注册接口
 * @param {*} 用户名，密码
 * @return {*}
 */
router.post("/register", (req, res) => {
  const { username, password } = req.body;
  const querySql = `SELECT * from user_info where username ="${username}"`;
  db(querySql, (err, result) => {
    if (err) {
      res.send(err);
    }
    if (result.length !== 0) {
      res.send({
        message: "用户名已存在",
        failed: true,
      });
    } else {
      const hasPwd = bcryptjs.hashSync(password, 10);
      const resgistSql = `INSERT INTO user_info (username, password,nickname) VALUES ('${username}','${hasPwd}','用户${username}')`;
      db(resgistSql, (err, result) => {
        if (err) {
          res.send(err);
        }
        if (result.affectedRows === 1) {
          const secret = "hand";
          // 3.生成token字符串
          const token = jsonToken.sign({ username: username }, secret, {
            expiresIn: "10h",
          });
          res.send({
            failed: false,
            message: "注册成功",
            token,
          });
        }
      });
    }
  });
});

module.exports = router;
